<template>
	<view>
		<view class="tform">
			<view class="photo_info">
				<view class="imag">
					<u-upload ref="uUpload" max-count="1" width="390" height="220" custom-btn :auto-upload="false"
						@on-choose-complete="afterRead">
						<view slot="addBtn" class="slot-btn slot-btn1">
							请上传证件照正面
						</view>
					</u-upload>
				</view>
			</view>
			<button class="submit" :ripple="true" :hair-line="false" @click="submit()">开始认证</button>
			<!-- <button class="submit" :ripple="true" :hair-line="false" @click="test()">test</button> -->
		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				sec: 1,
				imgurl: '',
				hinttext: '请拍摄或上传证件照原件，确保证件真实性！',
				param: {},
				imglist: [],
				form: {
					idBackImg: '',
					idFrontImg: '',
					name: '',
					id_number: ''
				}
			}
		},
		methods: {
			getBase64Image(img) {
				let canvas = document.createElement("canvas");
				canvas.width = img.width;
				canvas.height = img.height;
				let ctx = canvas.getContext("2d");
				ctx.drawImage(img, 0, 0, img.width, img.height);
				let ext = img.src.substring(img.src.lastIndexOf(".") + 1).toLowerCase();
				let dataURL = canvas.toDataURL("image/" + ext);
				console.log(dataURL)
				return dataURL;
			},
			afterRead(event) {
				// console.log(event[0].url)
				this.imgurl = event[0].url
			},
			submit() {
				let image = new Image();
				image.src = this.imgurl;
				// console.log(image)
				let base64 = this.getBase64Image(image);
				// console.log(base64);
				let data = {
					'api_key':'UIKKmSNTK88gLxtVZ6TUDeZ4Oi0QpNhc',
					'api_secret':'34ByLmVGJEq0Sr_7m87CPQLx6HLmhjcq',
					'image_base64':base64,
					'legality':1
				}
				uni.uploadFile({
					url: 'https://api-cn.faceplusplus.com/cardpp/v1/ocridcard',
					formData: data
				}).then(res =>{
					console.log(res[1].data)
				})

			},
			//页面跳转
			test() {
				// let formData  = new FormData();
				// formData.append('fileName', 'fileValue');
				let useData = new FormData();
				useData.append('idBackImg', this.imglist[1])
				useData.append('idFrontImg', this.imglist[0])
				useData.append('idNum', '11111')
				useData.append('userName', '2222')

				let postdata = {
					'idBackImg': this.imglist[1],
					'idFrontImg': this.imglist[0],
					'idNum': '11111',
					'userName': '2222'
				}
				console.log(formData)
				this.$axios({
					method: 'post',
					url: '/certification/idCertification',
					data: postdata,
					headers: {
						'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJvcGVuaWQiOiJvUE16VzVDTFduTEJmbkxfXzVFcWJZcjU3MndnIiwiZXhwIjoxNjQ4MjgyNTA4fQ.8YQKL-r_tlm7ghKlZdiv8Cl4JSChtFfFL5o09QIicXY'
					}
				}).then(res => {
					console.log(res)
				}).catch(err => {
					console.log(err)
				})
			},
			goto(url) {
				let turl = url + '?delta=3&' + 'successtext=保存成功';
				console.log(turl);
				uni.navigateTo({
					url: turl,
					animationType: 'slide-in-right',
					animationDuration: 1000

				})
			},
			setvalue(i) {
				this.sec = i;
			},
			beforeUpload(res) {
				console.log('上传前', res)
			}
		},
		onReady() { //生命周期
		}
	}
</script>

<style>
</style>
